﻿#include "imgbutton.h"

ImgButton::ImgButton(QWidget *parent)
    :QPushButton(parent), m_buttonColor(Qt::blue), m_textColor(Qt::white), m_backgroundOpacity(1.0),
      m_currentColor(Qt::blue), m_currentOpacity(1.0)
{
    setMinimumSize(80, 25); // 设置按钮最小尺寸
}


// 设置按钮颜色
void ImgButton::setButtonColor(const QColor &color)
{
    m_buttonColor = color;
    m_currentColor = m_buttonColor; // 更新当前颜色
    update(); // 触发重绘
}

// 设置按钮图片
void ImgButton::setButtonIcon(const QPixmap &icon, Qt::Alignment alignment)
{
    m_icon = icon;
    m_iconAlignment = alignment;
    update(); // 触发重绘
}

// 设置字体颜色
void ImgButton::setTextColor(const QColor &color)
{
    m_textColor = color;
    update(); // 触发重绘
}

// 设置背景透明度
void ImgButton::setBackgroundOpacity(qreal opacity)
{
    m_backgroundOpacity = opacity;
    m_currentOpacity = m_backgroundOpacity; // 更新当前透明度
    update(); // 触发重绘
}

// 重写 paintEvent 方法，绘制自定义按钮
void ImgButton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制背景
    QColor backgroundColor = m_currentColor;
    backgroundColor.setAlphaF(m_currentOpacity); // 设置背景透明度
    painter.setBrush(backgroundColor);
    painter.setPen(Qt::NoPen);

    // 绘制圆角矩形背景
    QRectF rect = this->rect();
    painter.drawRoundedRect(rect, 5, 5);

    // 绘制按钮图片
    if (!m_icon.isNull())
    {
        QRect iconRect;
        int iconSize = qMin(rect.width(), rect.height()) * 0.6; // 图片大小为按钮大小的 60%
        if (m_iconAlignment & Qt::AlignLeft)
        {
            iconRect = QRect(10, (rect.height() - iconSize) / 2, iconSize, iconSize); // 图片靠左
        }
        else if (m_iconAlignment & Qt::AlignRight)
        {
            iconRect = QRect(rect.width() - iconSize - 10, (rect.height() - iconSize) / 2, iconSize, iconSize); // 图片靠右
        }
        else if (m_iconAlignment & Qt::AlignCenter)
        {
            iconRect = QRect((rect.width() - iconSize) / 2, (rect.height() - iconSize) / 2, iconSize, iconSize); // 图片居中
        }
        painter.drawPixmap(iconRect, m_icon);
    }

    // 绘制按钮文本
    painter.setPen(m_textColor);
    painter.setFont(font());
    painter.drawText(rect, Qt::AlignCenter, text());
}

// 鼠标滑过事件
void ImgButton::enterEvent(QEvent *event)
{
    Q_UNUSED(event);
    m_currentColor = m_buttonColor.lighter(120); // 鼠标滑过时颜色变亮
    m_currentOpacity = 0.9; // 鼠标滑过时透明度降低
    update(); // 触发重绘
}

// 鼠标离开事件
void ImgButton::leaveEvent(QEvent *event)
{
    Q_UNUSED(event);
    m_currentColor = m_buttonColor; // 恢复默认颜色
    m_currentOpacity = m_backgroundOpacity; // 恢复默认透明度
    update(); // 触发重绘
}

// 鼠标点击事件
void ImgButton::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_currentColor = m_buttonColor.darker(120); // 鼠标点击时颜色变暗
        m_currentOpacity = 0.8; // 鼠标点击时透明度降低
        update(); // 触发重绘
    }
    QPushButton::mousePressEvent(event); // 调用基类方法，确保按钮的点击事件正常触发
}

// 鼠标释放事件
void ImgButton::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        if (rect().contains(event->pos()))
        {
            m_currentColor = m_buttonColor.lighter(120); // 鼠标在按钮内释放时颜色变亮
        }
        else
        {
            m_currentColor = m_buttonColor; // 鼠标在按钮外释放时恢复默认颜色
        }
        m_currentOpacity = 0.9; // 恢复鼠标滑过时的透明度
        update(); // 触发重绘
    }
    QPushButton::mouseReleaseEvent(event); // 调用基类方法，确保按钮的点击事件正常触发
}
